import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ModianVisitorLogApi } from '#/api/moxi/modianvisitorlog';
import Dayjs from 'dayjs';
import { handleTree } from '@vben/utils';

import { z } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
import { getSimplePostList } from '#/api/system/post';
import { getSimpleRoleList } from '#/api/system/role';
import {
    CommonStatusEnum,
    DICT_TYPE,
    getDictOptions,
    getRangePickerDefaultProps,
} from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
    return [
        {
            component: 'Input',
            fieldName: 'id',
            dependencies: {
                triggerFields: [''],
                show: () => false,
            },
        },
        {
            fieldName: 'username',
            label: '用户名称',
            component: 'Input',
            rules: 'required',
        },
        {
            label: '用户密码',
            fieldName: 'password',
            component: 'InputPassword',
            rules: 'required',
            dependencies: {
                triggerFields: ['id'],
                show: (values) => !values.id,
            },
        },
        {
            fieldName: 'nickname',
            label: '用户昵称',
            component: 'Input',
            rules: 'required',
        },
        {
            fieldName: 'deptId',
            label: '归属部门',
            component: 'ApiTreeSelect',
            componentProps: {
                api: async () => {
                    const data = await getDeptList();
                    return handleTree(data);
                },
                labelField: 'name',
                valueField: 'id',
                childrenField: 'children',
                placeholder: '请选择归属部门',
                treeDefaultExpandAll: true,
            },
        },
        {
            fieldName: 'postIds',
            label: '岗位',
            component: 'ApiSelect',
            componentProps: {
                api: getSimplePostList,
                labelField: 'name',
                valueField: 'id',
                mode: 'multiple',
                placeholder: '请选择岗位',
            },
        },
        {
            fieldName: 'email',
            label: '邮箱',
            component: 'Input',
            rules: z.string().email('邮箱格式不正确').or(z.literal('')).optional(),
        },
        {
            fieldName: 'mobile',
            label: '手机号码',
            component: 'Input',
        },
        {
            fieldName: 'sex',
            label: '用户性别',
            component: 'RadioGroup',
            componentProps: {
                options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
                buttonStyle: 'solid',
                optionType: 'button',
            },
            rules: z.number().default(1),
        },
        {
            fieldName: 'status',
            label: '用户状态',
            component: 'RadioGroup',
            componentProps: {
                options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
                buttonStyle: 'solid',
                optionType: 'button',
            },
            rules: z.number().default(CommonStatusEnum.ENABLE),
        },
        {
            fieldName: 'remark',
            label: '备注',
            component: 'Textarea',
        },
    ];
}







/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'visitorName',
            label: '预约单号',
            component: 'Input',
            componentProps: {
                placeholder: '请输入员工姓名',
                allowClear: true,
            },
        },

        {
            fieldName: 'visitorMobile',
            label: '访客手机号',
            component: 'Input',
            componentProps: {
                placeholder: '请输入访客手机号',
                allowClear: true,
            },
        },
        {
            fieldName: 'requstStatus',
            label: '预约状态',
            component: 'Select',
            componentProps: {
                options: [
                    { label: '已提交', value: 0 },
                    { label: '已完成', value: 1 },
                    { label: '已取消', value: 2 }
                ],
                placeholder: '请选择状态',
                clearable: true,
            },
        },
    ];
}

/** 列表的字段 */
export function useGridColumns<T = ModianVisitorLogApi.ModianVisitorLog>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'visitorName',
            title: '访客姓名',
        },
        {
            field: 'visitorMobile',
            title: '访客手机号',
        },
        {
            field: 'startTime',
            title: '到访时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'endTime',
            title: '离开时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'visitorFace',
            title: '头像',
            cellRender: {
                name: 'CellImage',
            },
        },
        {
            field: 'visitorIdcard',
            title: '访客身份证号',
        },
        {
            field: 'respondentId',
            title: ' 被访人名称',
            formatter: ({ row }) => {
                return row ? row.respondent.name : '';
            },
        },
        {
            field: 'addressId',
            title: '到访部门',
            formatter: ({ row }) => {
                return row?.respondent?.deptNames?.split(',').pop()?.trim() || '';
            },
        },
        {
            field: 'visitCause',
            title: ' 来访目的',
        },
        {
            field: 'requstStatus',
            title: '预约状态',
            formatter: ({ cellValue }) => {
                const statusMap = {
                    0: '已提交',
                    1: '已完成',
                    2: '已取消'
                };
                return statusMap[cellValue as keyof typeof statusMap] || '未知状态';
            },
        },
        {
            field: 'approvalStatus',
            title: '审批状态',
            formatter: ({ cellValue }) => {
                const statusMap = {
                    0: '审批中',
                    1: '通过',
                    2: '驳回'
                };
                return statusMap[cellValue as keyof typeof statusMap] || '未知状态';
            },
        },
        {
            field: 'sendSms',
            title: '是否发送短信',
        },
        {
            field: 'userType',
            title: '用户类',
        },
        {
            field: 'visitorCompany',
            title: '访客公司',
        },
        {
            title: '操作',
            width: 180,
            fixed: 'right',
            slots: { default: 'actions' },
        },
    ];
}
